/**
 * 平台考核
 */

var vehicle;
var enterprise;
$(function(){
	PlatformAnalyzes.init();
});

var PlatformAnalyzes = {
param: {},
init: function(){
	var me = this;
	me.initQueryTime();
	me.loadTableTitle();
	me.queryTable();
	me.initweek();
	me.initPlatform();
},
	
//初始化时间
initQueryTime : function(){
    var todayDate = new Date();
    var todayDateStr=todayDate.getFullYear()+"-"+(todayDate.getMonth()+1)+"-"+todayDate.getDate();  
    var beforeDate = new Date(todayDate-86400000*1); //前一天
    var yyyy = beforeDate.getFullYear(), mm = (beforeDate.getMonth() + 1).toString(), dd = beforeDate.getDate().toString();
    if (mm.length == 1) {
        mm = '0' + mm;
    }
    if (dd.length == 1) {
        dd = '0' + dd;
    }
    $("#day").val(yyyy + '-' + mm + '-' + dd);
    beforeDate.setMonth(beforeDate.getMonth()-1);
    yyyy = beforeDate.getFullYear(), mm = (beforeDate.getMonth() + 1).toString();
    if (mm.length == 1) {
        mm = '0' + mm;
    }
    $("#month").val(yyyy + '-' + mm );
},
//初始化周维度
initweek : function(){
	$.ajax({
		type : 'post',
		url : appCtx + "base/report/PlatformAnalyzes/queryweek.html",
		cache : false,
		dataType : "JSON",
		success : function(data) {
			if(data!=null&&data!=''){
				$.each(data,function(entryIndex,entry) {
					$("#week").append("<option value='"+entry.value+"'>"+entry.text+"</option>");
				});				
			}
		}
	});
},
getColumn:function(){
	return [{
        display : 'gnss_centerid',
        name : 'gnss_centerid',
        hide:true,
    },{
        display : '排名',
        name : 'ranking',
        width : 3,
        sortable : false,
        align : 'left',
        showTitle : 'ranking'
    },{
        display : '得分',
        name : 'score',
        width : 3,
        sortable : false,
        align : 'left',
        showTitle : 'score'
    },{
        display : '平台名称',
        name : 'platform_name',
        width : 15,
        sortable : false,
        align : 'left',
        showTitle : 'platform_name'
    },{
        display : '平台连通率(%)',
        name : 'online_rate',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'online_rate',
        paramcolnames:['online_rate'],
        handlefunction:function(online_rate){
        	var handleStr;
        	if(online_rate==0){
        		handleStr="0"
        	}else{
        		handleStr =Math.round((online_rate)*Math.pow(10,2))/Math.pow(10,2);
        	}
    		return handleStr;
        }
    },{
        display : '车辆总数',
        name : 'vehicle_count',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'vehicle_count'
    },{
        display : '入网车辆数',
        name : 'vehicle_access_count',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'vehicle_access_count'
    },{
        display : '入网率(%)',
        name : 'vehicle_access_rate',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'vehicle_access_rate',
        paramcolnames:['vehicle_access_count','vehicle_count'],
        handlefunction:function(vehicle_access_count,vehicle_count){
        	var handleStr;
        	if(vehicle_access_count==0){
        		handleStr="0"
        	}else{
        		handleStr =Math.round((vehicle_access_count/vehicle_count*100)*Math.pow(10,2))/Math.pow(10,2);
        	}
    		return handleStr;
        } 
    },{
        display : '上线率(%)',
        name : 'vehicle_online_rate',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'vehicle_online_rate',
        paramcolnames:['vehicle_online_count','vehicle_access_count'],
        handlefunction:function(vehicle_online_count,vehicle_access_count){
        	var handleStr;
        	if(vehicle_access_count==0){
        		handleStr="0"
        	}else{
            	handleStr =Math.round((vehicle_online_count/vehicle_access_count*100)*Math.pow(10,2))/Math.pow(10,2);
        	}
    		return handleStr;
        }
    },{
        display : '数据合格率(%)',
        name : 'data_pass',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'data_pass',
        paramcolnames:['exception_track_count','track_normal'],
        handlefunction:function(exception_track_count,track_normal){
        	var handleStr;
        	var count = (Number(track_normal)+Number(exception_track_count));
        	var param = 1-Number(exception_track_count)/count;
        	if(count==0){
        		handleStr="0"
        	}else{
        		handleStr = handleStr =Math.round((param*100)*Math.pow(10,2))/Math.pow(10,2);	
        	}
    		return handleStr;
        }
    },{
        display : '轨迹完整率(%)',
        name : 'track_complete_rate',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'track_complete_rate',
        paramcolnames:['track_complete_rate'],
        handlefunction:function(track_complete_rate){
        	var handleStr =Math.round((track_complete_rate)*Math.pow(10,2))/Math.pow(10,2);	
    		return handleStr;
        }
    },{
        display : '定位漂移车辆比率(%)',
        name : 'travel',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'travel',
        paramcolnames:['exception_vehicle_count','location_exception_count','speed_160_count','gpstime_exception_count','head_exception_count','vehicle_online_count'],
        handlefunction:function(exception_vehicle_count,location_exception_count,speed_160_count,gpstime_exception_count,head_exception_count,vehicle_online_count){
        	var handleStr;
        	var sum =Number(location_exception_count)+Number(speed_160_count)+Number(gpstime_exception_count)+Number(head_exception_count);
        	var avg = sum<=0?0:location_exception_count/sum;
        	handleStr =vehicle_online_count<=0?0:(avg*exception_vehicle_count)/vehicle_online_count;
        	handleStr =Math.round((handleStr*100)*Math.pow(10,2))/Math.pow(10,2);
/*            	if(vehicle_online_count==0){
            		handleStr="0"
            	}else{
            		var param = Number(location_exception_count)/Number(vehicle_online_count);
            		handleStr =Math.round((param*100)*Math.pow(10,2))/Math.pow(10,2);	
            	}  演示完毕后添加经纬度异常车辆数  */
    		return handleStr;
        }
    /*},{
        display : '安全报警',
        name : 'adas_alarm_count',
        width : 9,
        sortable : false,
        align : 'left',
        showTitle : 'travel',*/
    }]
},

loadTableTitle:function(){
	$("#objTable").flexigrid({
            dataType : 'json',
            colModel :this.getColumn() ,
            usepager : false,// 是否分页，默认为true。
            singleSelect:true,
            height:window.parent.nav.whiceH[1]-177,
            width: window.parent.$("iframe").width(),
            widthType:'%',
            rp : 10,
            onSelectRow:function(row){
            	var startTime = $("#inputStartDate").val();
            	var endTime = $("#inputEndDate").val();
            	switchoverfow(row.gnss_centerid);
//            	window.open(global.url.domain+'gps-web/base/report/PlatformAnalyzes/toqueryPlatformAnalyzes.html?platfromid='+row.gnss_centerid+'&startTime='+startTime+'&endTime='+endTime);
            }
     });
},
queryTable:function(){
	var me = this;
	var veidoo = $("#veidoo").val();
	var analyse_date = null;
	var gnss_centerid = null;
	if(platform_id!=null&&platform_id!=''){
		gnss_centerid = platform_id;
	}else{
		gnss_centerid = $("#platform_id").val();
	}
	if(veidoo=='day'){
		analyse_date = $("#day").val();
	}else if(veidoo=='week'){
		analyse_date = $("#week").val();
	}else if(veidoo=='month'){
		analyse_date = $("#month").val();
	}
	$.ajax({
		url:appCtx+"base/report/PlatformAnalyzes/queryPlatformAnalyzes.html",
		type:"post",
		cache : false,
		data:{
			veidoo:veidoo,
			analyse_date:analyse_date,
			gnss_centerid:gnss_centerid,
		},
		dataType:"json",
		success:function(data){
        		if(data.listAnalyzes.length==0){
    				var dataRows= {
    			     		rows:"",
    			     		totol:0,
    			     		page:1
    			     	};
    		     	$("#objTable").flexAddData(dataRows);
    			}else{
					/*给所有table赋值*/
    		     	var dataRows= {
    			     		rows:data.listAnalyzes,
    			     		totol:data.length,
    			     		page:1
    			     	};
    		    $("#objTable").flexAddData(dataRows);
			}
		}
	});
},
    
exportExcel : function(){
	var me = this;
	var total = $("#objTable").getOptions().total;
    if (total == 0) {
    	window.parent.global.ajaxLoading.pop("无导出数据", false, 2000);
        return;
    }
    //总记录数
    window.parent.global.ajaxLoading.pop("正在导出,请稍后.....", true, 2000);
	var header1 = JSON.stringify(me.getColumn());
	var headerbox1 = JSON.parse(header1);
	var gnss_centerid=null;
	var headerWidth1 = $("#objTable").width();
	for(var i=0;i<headerbox1.length;i++){
		headerbox1[i].width = Math.ceil(headerWidth1 / 100 * headerbox1[i].width);
	}		
	if(platform_id!=null&&platform_id!=''){
		gnss_centerid = platform_id;
	}else{
		gnss_centerid = $("#platform_id").val();
	}
	headerbox1.splice(headerbox1.length,1);
	var headerend1 = JSON.stringify(headerbox1);
	 
	//表头
	var colModelList=new Array();
	colModelList.push(headerend1);
	var colModel= $.toJSON(colModelList);
	var veidoo = $("#veidoo").val();
	var analyse_date = null;
	if(veidoo=='day'){
		analyse_date = $("#day").val();
	}else if(veidoo=='week'){
		analyse_date = $("#week").val();
	}else if(veidoo=='month'){
		analyse_date = $("#month").val();
	}
	var param =  {
			veidoo:veidoo,
			analyse_date:analyse_date,
			gnss_centerid:gnss_centerid,
			colModel:colModel,
			title:"平台分析考核表"
	};
	post(appCtx+"base/report/PlatformAnalyzes/QCexport.html",param);
},
initPlatform:function(){
	var platfromName = window.parent.platfromName;
	if(platfromName!=null&&''!= platfromName){
		$('#platform_id').html('<option>'+platfromName+'</option>');
	}
	$('#platform_id').select2({
		theme: "default",
	        allowClear: true,
	        placeholder: "请选择平台",
	        ajax:{
	            url: appCtx + "base/report/PlatformAnalyzes/queryPlatform.html",
	            dataType:"json",
	            delay:250,
	           async : true,
	            data:function(params){
	                return {
	                    name: params.term,
	                    page: params.page || 1
	                };
	            },
	            cache:true,
	            processResults: function (res, params) {
			    	if(res){
			    		var options = [];
			    		var option = {"id":" ", "text":"请选择平台"};
			    		options.push(option);
	    				$.each(res,function(index,obj) {
	    					var option = {"id":obj.gnss_centerid, "text":obj.text};
 	                    options.push(option);
	    				});
 	                return {
 	                	results: options,
 	                    pagination: {
 	                        more:false
 	                    }
 	                };
			    	}
	            },
	            escapeMarkup: function (markup) { return markup; },
	            minimumInputLength: 1
	        }
	})
	
	/*$.ajax({
		type : 'post',
		url : appCtx + "base/report/PlatformAnalyzes/queryPlatform.html",
		cache : false,
		dataType : "JSON",
		success : function(data) {
			if(data!=null&&data!=''){
				$('#vehicleSelDiv').html("<select class='query-select' id='platform_id'  onchange='cleanplatformId()' ></select>");
				var selectStr="";
				selectStr+="<option value=''>请选择平台</option>";
				$.each(data,function(entryIndex,entry) {
					if(platform_id==entry.gnss_centerid){
						selectStr+="<option value='"+entry.gnss_centerid+"'  selected='selected'>"+entry.text+"</option>";
					}else{
						selectStr+="<option value='"+entry.gnss_centerid+"'>"+entry.text+"</option>";
					}
				});
				$('#platform_id').html(selectStr);
				$("#platform_id").select2();					
			}
		}
	});*/
},
};
function selectveidoo(){
	var vo = $("#veidoo").val();
	if(vo=='day'){
		$("#day").show();
		$("#week").hide();
		$("#month").hide();
	}else if(vo=='week'){
		$("#day").hide();
		$("#week").show();
		$("#month").hide();
	}else if(vo=='month'){
		$("#day").hide();
		$("#week").hide();
		$("#month").show();
	}
}
function cleanplatformId(){
	platform_id='';
}

function switchoverfow(platform_id){
	window.parent.$("#menuContAL").show();
	window.parent.$("#menuContQC").hide();
	window.parent.$("#AL").attr("class","fl active");
	window.parent.$("#QC").attr("class","fl");
	
	var veidoo = $('#veidoo').val();
	var analyse_date = null;
	if(veidoo=='day'){
		analyse_date = $("#day").val();
	}else if(veidoo=='week'){
		analyse_date = $("#week").val();
	}else if(veidoo=='month'){
		analyse_date = $("#month").val();
	}
	window.parent.frames[0].callback(veidoo,platform_id,analyse_date);
}